package com.insectstudio.haiivloffical;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.androidquery.AQuery;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.insectstudio.haiivloffical.adapter.MenuAdapter;
import com.insectstudio.haiivloffical.model.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MyActivity extends SlidingFragmentActivity {
	
	private ListView listMenu;
	protected AdView mAdView;
	protected InterstitialAd interstitial;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// set menu
		setBehindContentView(R.layout.menu);
		// get references
		listMenu = (ListView) findViewById(R.id.list_menu);
		// set content of menu
		ArrayList<MenuItem> menuItems = new ArrayList<MenuItem>();
		menuItems.add(new MenuItem(Constants.NEW_ID, getString(R.string.NewImage)));
		menuItems.add(new MenuItem(Constants.HOT_ID, getString(R.string.HotImage)));
		menuItems.add(new MenuItem(Constants.OLD_ID, getString(R.string.OldImage)));
		menuItems.add(new MenuItem(Constants.BROWSING_ID, getString(R.string.BrowsingImage)));
		menuItems.add(new MenuItem(Constants.VIDEO_ID, getString(R.string.Videos)));
		MenuAdapter adapter = new MenuAdapter(MyActivity.this, R.layout.menu_item, menuItems, new AQuery(MyActivity.this));
		listMenu.setAdapter(adapter);
		// set on item click listener
		listMenu.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				int menuId = ((MenuItem) parent.getAdapter().getItem(position)).getId();
				toggle();
				switch (menuId) {
				case Constants.NEW_ID:
					Intent i = new Intent(MyActivity.this, CategoryActivity.class);
					i.putExtra("title", getString(R.string.NewImage));
					i.putExtra("url", Constants.NEW_METHOD);
					i.putExtra("page", 1);
					startActivity(i);
					finish();
					break;
				case Constants.HOT_ID:
					i = new Intent(MyActivity.this, CategoryActivity.class);
					i.putExtra("title", getString(R.string.HotImage));
					i.putExtra("url", Constants.HOT_METHOD);
					i.putExtra("page", 1);
					startActivity(i);
					finish();
					break;
				case Constants.OLD_ID:
					i = new Intent(MyActivity.this, CategoryActivity.class);
					i.putExtra("title", getString(R.string.OldImage));
					i.putExtra("url", Constants.OLD_METHOD);
					i.putExtra("page", 1);
					startActivity(i);
					finish();
					break;
				case Constants.BROWSING_ID:
					i = new Intent(MyActivity.this, CategoryActivity.class);
					i.putExtra("title", getString(R.string.BrowsingImage));
					i.putExtra("url", Constants.BROWSING_METHOD);
					i.putExtra("page", 1);
					startActivity(i);
					finish();
					break;
				case Constants.VIDEO_ID:
					i = new Intent(MyActivity.this, CategoryActivity.class);
					i.putExtra("title", getString(R.string.Videos));
					i.putExtra("url", Constants.VIDEO_METHOD);
					i.putExtra("page", 1);
					startActivity(i);
					finish();
					break;
				}
				
			}
		});
		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// set mode
		sm.setMode(SlidingMenu.LEFT);
	}
	
	public void displayInterstitial() {		
		interstitial = new InterstitialAd(MyActivity.this);
		interstitial.setAdUnitId(getString(R.string.interstitial_banner_ad_unit_id));
		interstitial.setAdListener(new AdListener() {
			@Override
			public void onAdLoaded() {
				// TODO Auto-generated method stub
				super.onAdLoaded();
		        interstitial.show();
		        }
			}); 
		// Create ad request.
		AdRequest adRequest = new AdRequest.Builder().build();
		// Begin loading your interstitial.
		interstitial.loadAd(adRequest);

	}

	public void showAlertDialog(String title, String message) {
		new AlertDialog.Builder(MyActivity.this).setTitle(title)
				.setMessage(message)
				.setNeutralButton(getString(R.string.Ok), new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();

					}
				}).create().show();
	}

	public String getDirectUrl(String url , int page)
	{
		String newUrl = "http://haiivl.com/"; //moi/1
		if (url.contains("moi"))
		{
			newUrl = newUrl + "moi/" + page ;
		}
		if (url.contains("cu"))
		{
			newUrl = newUrl + "cu/" + page ;
		}
		if (url.contains("hot"))
		{
			newUrl = newUrl + "hot/" + page ;
		}
		if (url.contains("duyet"))
		{
			newUrl = newUrl + "duyet/" + page ;
		}
		if (url.contains("videos"))
		{
			newUrl = newUrl + "videos/" + page ;
		}
		return newUrl ;
	}
}
